package com.ghh.app.monsterrush.monsters.state;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.ghh.app.monsterrush.MonsterActor;
import com.ghh.app.monsterrush.StateAction;

public class DestroyState implements StateAction {
	private float			stateTime	= 0f;
	private MonsterActor	monster;
	private TextureRegion	textureRegion;
	private Animation		animation;

	public DestroyState(MonsterActor monster, Texture texture) {
		this.monster = monster;
		this.textureRegion = new TextureRegion(texture);
	}

	public DestroyState(MonsterActor monster, TextureRegion[] frames, float frameDuration) {
		this.monster = monster;
		this.animation = new Animation(frameDuration, frames);
		this.animation.setPlayMode(Animation.NORMAL);
	}
	
	@Override
	public void start() {
		stateTime = 0f;
	}

	@Override
	public void act(float deltaTime) {
		stateTime += deltaTime;
		if (animation != null) {
			if (animation.isAnimationFinished(stateTime)) {
				monster.onDestroyed();
			}
		} else {
			monster.onDestroyed();
		}
	}

	@Override
	public void draw(SpriteBatch batch) {
		if (textureRegion != null) {
			monster.drawFrame(batch, textureRegion);
		} else {
			monster.drawFrame(batch, animation.getKeyFrame(stateTime));
		}
	}

}
